﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SuperFrame.Charts
{
    public class ContainerSpline : SeriesContainer
    {
        public override void Draw()
        {
            base.Draw();
        }

        protected override void DrawPath(BaseSeries series, System.Drawing.Pen pen)
        {
            if (series is SeriesSpline)
            {
                SeriesSpline SeriesSpline = series as SeriesSpline;
                if (SeriesSpline == null || SeriesSpline.SplinePoints == null || SeriesSpline.SplinePoints.Count <= 0)
                {
                    return;
                }
                if (RenderingMode == RenderingMode.Default)
                {
                    PartsCanvas.Children.Clear();
                    PathFigure pf = new PathFigure() { StartPoint = SeriesSpline.SplinePoints[0] };
                    PathFigureCollection pfc = new PathFigureCollection();
                    pfc.Add(pf);

                    if (SeriesSpline.IsShowPoint)
                        PartsCanvas.Children.Add(SeriesSpline.Parts[0].CreatePoint());
                    for (int i = 1; i <= SeriesSpline.Parts.Count-1 ; i++)
                    {
                        // PartsCanvas.Children.Add(SeriesSpline.Parts[i].CreatePart());
                        pf.Segments.Add(SeriesSpline.Parts[i].CreatePath());
                        if (SeriesSpline.IsShowPoint)
                            PartsCanvas.Children.Add(SeriesSpline.Parts[i].CreatePoint());
                    }
                    //if (SeriesSpline.IsShowPoint && SeriesSpline.Parts.Count - 1 >= 0)
                    //    PartsCanvas.Children.Add(SeriesSpline.Parts[SeriesSpline.Parts.Count - 1].CreateEndPoint());

                    PathGeometry pg = new PathGeometry(pfc);
                    Path splinePath = new Path();
                    splinePath.Stroke = SeriesSpline.Parts[0].Stroke;
                    splinePath.StrokeThickness = 1;
                    splinePath.Data = pg;
                    PartsCanvas.Children.Add(splinePath);

                }
                else
                {
                    var pointCount = SeriesSpline.SplinePoints.Count;
                    for (int i = 0; i < pointCount - 1; i++)
                    {
                        switch (RenderingMode)
                        {
                            case RenderingMode.GDIRendering:
                                //GDIGraphics.DrawBezier(pen, points[i].AsDrawingPointF(), SeriesSpline.FirstControlPoints[i].AsDrawingPointF(), SeriesSpline.SecondControlPoints[i].AsDrawingPointF(), points[i + 1].AsDrawingPointF());
                                break;
                            case RenderingMode.Default:
                                break;
                            case RenderingMode.WritableBitmap:
                                this.WritableBitmap.Lock();
                                //WritableBitmapGraphics.DrawBezier(pen, points[i].AsDrawingPointF(), SeriesSpline.FirstControlPoints[i].AsDrawingPointF(), SeriesSpline.SecondControlPoints[i].AsDrawingPointF(), points[i + 1].AsDrawingPointF());
                                this.WritableBitmap.AddDirtyRect(new Int32Rect(0, 0, WritableBitmap.PixelWidth, WritableBitmap.PixelHeight));
                                this.WritableBitmap.Unlock();
                                break;
                            default:
                                break;
                        }
                    }
                    this.collection.InvalidateBitmap();
                }
            }
        }

    }
}
